package com.http.filter;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@WebServlet(name = "RegisterServlet", value = "/RegisterServlet") // 注册 Servlet 的 URL
public class RegisterServlet extends HttpServlet {

    // 用于存储用户信息（示例中使用的内存存储）
    private Map<String, String> userDatabase = new HashMap<>();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        // 获取注册表单提交的用户名密码
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        // 将用户名密码设置进session的属性
        HttpSession session = request.getSession();
        session.setAttribute("username", username);
        session.setAttribute("password", password);
        // 重定向到登录页面
        response.sendRedirect("/pub/login.html");
    }

    // 辅助方法：打印消息到响应
    private void printMessage(HttpServletResponse response, String message) throws IOException {
        PrintWriter out = response.getWriter();
        out.println("<html><body>");
        out.println("<h3>" + message + "</h3>");
        out.println("</body></html>");
    }
}
